package com.example.zhaoyulu.yunzhiapp;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.example.zhaoyulu.yunzhiapp.fragement.gerenxinxiFragment;
import com.example.zhaoyulu.yunzhiapp.fragement.quanziFragment;
import com.example.zhaoyulu.yunzhiapp.fragement.zixunFragment;

public class MainActivity extends zylBaseActivity {
    Button main_zixun;
    Button main_quanzi;
    Button main_gerenxinxi;
    Button main_search;
    TextView main_tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);
        main_tv=findViewById(R.id.main_tv);
        init();
        setview();
        main_search.setVisibility(View.GONE);
        main_zixun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FM.beginTransaction().show(zixunFragment).hide(quanziFragment).hide(gerenxinxiFragment).commitAllowingStateLoss();

                main_zixun.setBackgroundResource(R.mipmap.zixuntrue);
                main_quanzi.setBackgroundResource(R.mipmap.quanzifalse);
                main_gerenxinxi.setBackgroundResource(R.mipmap.personalfalse);
                main_tv.setText("资讯");
                main_search.setVisibility(View.VISIBLE);

                //main_invide_guest.setBackgroundResource(R.mipmap.churu);
                //main_outAndIN.setBackgroundResource(R.mipmap.yaoqing);
                //main_addSon.setBackgroundResource(R.mipmap.mianbu);
            }
        });

        main_quanzi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FM.beginTransaction().show(quanziFragment).hide(gerenxinxiFragment).hide(zixunFragment).commitAllowingStateLoss();
                main_zixun.setBackgroundResource(R.mipmap.zixunfalse);
                main_quanzi.setBackgroundResource(R.mipmap.quanzitrue);
                main_gerenxinxi.setBackgroundResource(R.mipmap.personalfalse);
                main_search.setVisibility(View.GONE);
                main_tv.setText("圈子");
            }
        });

        main_gerenxinxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FM.beginTransaction().show(gerenxinxiFragment).hide(quanziFragment).hide(zixunFragment).commitAllowingStateLoss();

                main_zixun.setBackgroundResource(R.mipmap.zixunfalse);
                main_quanzi.setBackgroundResource(R.mipmap.quanzifalse);
                main_gerenxinxi.setBackgroundResource(R.mipmap.personnaltue);
                main_tv.setText("个人信息");
                main_search.setVisibility(View.GONE);
                //Intent intent=new Intent(getApplicationContext(),com.example.zhaoyulu.yunzhiapp.jiaowu.ui.MainActivity.class);
                //startActivity(intent);

            }
        });

        main_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,SearchActvity.class);
                startActivity(intent);
            }
        });
    }

    private void init(){
        main_zixun=findViewById(R.id.main_zixun);
        main_quanzi=findViewById(R.id.main_quanzi);
        main_gerenxinxi=findViewById(R.id.main_gerenxinxi);
        main_search=findViewById(R.id.main_search);
    }


    gerenxinxiFragment gerenxinxiFragment;
    quanziFragment  quanziFragment;
    zixunFragment zixunFragment;
    FragmentManager FM = getFragmentManager();
    private void setview() {
        FragmentTransaction MfragmentTransaction;

        //获取到FragmentManager，在V4包中通过getSupportFragmentManager，
        //在系统中原生的Fragment是通过getFragmentManager获得的。
        //2.开启一个事务，通过调用beginTransaction方法开启。
        MfragmentTransaction=FM.beginTransaction();
        //把自己创建好的fragment创建一个对象
        gerenxinxiFragment= new gerenxinxiFragment();
        quanziFragment= new quanziFragment();
        zixunFragment=new zixunFragment();

        //向容器内加入Fragment，一般使用add或者replace方法实现，需要传入容器的id和Fragment的实例。
        MfragmentTransaction.add(R.id.main_Fragment,gerenxinxiFragment);
        MfragmentTransaction.add(R.id.main_Fragment,quanziFragment);
        MfragmentTransaction.add(R.id.main_Fragment,zixunFragment);

        //提交事务，调用commit方法提交。
        MfragmentTransaction.show(quanziFragment).hide(gerenxinxiFragment).hide(zixunFragment);


        MfragmentTransaction.commitAllowingStateLoss();

    }
}
